package com.yanqu.road.server.cmd.vehicle;

import com.yanqu.road.logic.pb.VehiclePb;
import com.yanqu.road.entity.config.vehicle.VehicleModifySkillResult;
import com.yanqu.road.pb.vehicle.VehicleProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_VEHICLE_SKILL_MODIFY, desc = "座骑技能改装(随机技能)")
public class VehicleSkillModifyCmd extends AbstractCommand {

	String vehicleUid;

	int skillPos;

	boolean isCustomize;

	VehicleModifySkillResult result;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		VehicleProto.VehicleSkillModifyReqMsg reqMsg = VehicleProto.VehicleSkillModifyReqMsg.parseFrom(packet.getBytes());
		vehicleUid = reqMsg.getVehicleUid();
		skillPos = reqMsg.getSkillPos();
		isCustomize = reqMsg.getIsCustomize();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		result = player.getModule(VehicleModule.class).randomSkill(vehicleUid, skillPos, isCustomize, false);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(null != result) {
			VehicleProto.VehicleSkillModifyRespMsg.Builder respMsg = VehicleProto.VehicleSkillModifyRespMsg.newBuilder();
			respMsg.setRet(result.getRet());
			if (0 == result.getRet()) {
				respMsg.setVehicleUid(vehicleUid);
				VehicleProto.VehicleSkillTempMsg.Builder skillMsg = VehiclePb.parseVehicleSkillTempMsg(result.getVehicleSkill());
				if(null != skillMsg) {
					respMsg.setSkill(skillMsg);
				}
			}
			player.sendPacket(Protocol.U_VEHICLE_SKILL_MODIFY, respMsg);
		}
	}
}
